package com.itgaoshu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.bean.*;
import com.itgaoshu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * \* @author: 李豪杰
 * \* Date: 2021/3/21
 * \* Time: 14:03
 * \* Description:
 * \
 */
@Controller
@RequestMapping("seldrugstore")
public class DrugdirectoryController {
    @Autowired
    private DrugdictionaryService drugdictionaryService;
    @Resource
    private PutinService putinService;
    @Resource
    private DrugStoreService drugStoreService;
    @Resource
    private JiLuservice jiLuservice;
    /**
     * 查询准备入库的药品
     */
    @ResponseBody
    @RequestMapping("selectdgty")
    public Object selectAllDrugs(Model model,Drugdictionary drugdictionary, Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Drugdictionary> drugdictionaries = drugdictionaryService.selectAllDrugs(drugdictionary);
        PageInfo pageInfo = new PageInfo(drugdictionaries);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    //查询选中的药品的库存数量
    @RequestMapping("selnumber")
    @ResponseBody
    public int selnumber(Drugstore drugstore) {
        int number = putinService.selectNumber(drugstore);
        return number;
    }
    //查询药品类型
    @RequestMapping("seltype")
    @ResponseBody
    public Object seltype(Type type) {
        List<Type> seltype = drugStoreService.seltype(type);
        return seltype;
    }
    //新增药品
    @RequestMapping("adddrugs")
    @ResponseBody
    public Integer addDrugs(Drugstore drugstore,Jilu jilu){//添加药品
        System.out.println(drugstore);
        Integer updrugnumber=0;
        Integer adddrugstore=0;
        int seldrugname = putinService.seldrugname(drugstore);
        if(seldrugname==1){
            updrugnumber = putinService.updrugnumber(drugstore);
            int addjilu = jiLuservice.addjilu(jilu);//添加一条记录

        }
        if (seldrugname!=1){
            System.out.print("进入添加方法");
            adddrugstore = drugStoreService.addDrugs(drugstore);
            System.out.print("添加记录表1");
            int addjilu = jiLuservice.addjilu(jilu);//添加一条记录

        }
        return adddrugstore+updrugnumber;
    }
    //查询计量单位
    @RequestMapping("selunit")
    @ResponseBody
    public Object selunit(Unit unit) {
        List<Unit> selunit = drugStoreService.selunit(unit);
        return selunit;
    }
    //查询产地
    @RequestMapping("selarea")
    @ResponseBody
    public Object selarea(Area area) {
        List<Area> selarea = drugStoreService.selarea(area);
        return selarea;
    }
    //查询经办人
    @RequestMapping("selskull")
    @ResponseBody
    public Object selskull(Skull skull){
        List<Skull> selskull = drugStoreService.selskull(skull);
        return selskull;
    }
    //查询供货商
    @RequestMapping("selupplier")
    @ResponseBody
    public Object selupplier(Upplier upplier) {
        List<Upplier> selupplier = drugStoreService.selupplier(upplier);
        return selupplier;
    }
    //查询药品仓库
    @RequestMapping("selectdrugstore")
    @ResponseBody
    public Object seldrugstore(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = drugStoreService.selStore(drugstore);
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        drugstoresData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //查询仓库里药品数量不足的药品
    @RequestMapping("selectlackdrug")
    @ResponseBody
    public Object selectlackdrug(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = drugStoreService.selectlackdrug(drugstore);//查询药品数量不足的药
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        drugstoresData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //查询采购单
    @RequestMapping("selcaigou")
    @ResponseBody
    public Object selcaigou(Caigou caigou, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Caigou> caigous = drugStoreService.selcaigou(caigou);//查询遍历采购表
        PageInfo pageInfo = new PageInfo(caigous);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        drugstoresData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }

    //添加一条药品采购单
    @RequestMapping("addcaigou")
    @ResponseBody
    public Integer addcaigou(Caigou caigou) {
        int selcaigouname = drugStoreService.selcaigouname(caigou);//查询采购单是是否已经存在此条数据
        if(selcaigouname==0){
            int seldrugname =drugStoreService.addcaigou(caigou);//添加
            return seldrugname;

        }else {
            int upcaigounumber = drugStoreService.upcaigounumber(caigou);//修改
            return upcaigounumber;
        }

    }
    //删除一条药品采购单
    @RequestMapping("delcaigou")
    @ResponseBody
    public Integer delcaigou(Integer caigouid) {
        int seldrugname =drugStoreService.delcaigou(caigouid);//删除此条采购数据
        return seldrugname;
    }
    //查询过期的药都有哪些
    @RequestMapping("seldrugDateguoqi")
    @ResponseBody
    public Object seldrugDateguoqi(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = drugStoreService.seldrugDate(drugstore);
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        drugstoresData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }
    //删除过期的药
    @RequestMapping("delguoqidurg")
    @ResponseBody
    public Integer delguoqidurg(Integer rugstoreId,Jilu jilu) {
        System.out.print("进入删除");
        int delguoqidurg = drugStoreService.delguoqidurg(rugstoreId);//删除此条数据
        if(delguoqidurg==1){//如果删除此条 则添加到记录表

            System.out.print("添加记录表");
            int addjilu = jiLuservice.addjilu(jilu);
        }
        return delguoqidurg;
    }
}
